using System.Web; 
using System.Text.RegularExpressions; 
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
namespace IWeb_Core_Connector
{
    public partial class IWeb_Form  
    { 
        public IWeb_Form() 
        { 
            InitializeComponent();if (transDefaultFormIWeb_Form == null)	transDefaultFormIWeb_Form = this;
        }
        public IWeb_Core_Connector.IWebservice.WebService IWeb = new IWeb_Core_Connector.IWebservice.WebService(); 
        
        #region '"Properties"' 
        public string WebserviceURL 
        { 
            get 
            { 
                return this.txtWebsite.Text + "/DesktopModules/IWebCSharp/webservice.asmx"; 
            } 
        } 
        public string Password 
        { 
            get 
            { 
                string strPassword = ""; 
                if ( this.chkEncrypted.Checked ) 
                { 
                    IWebEncryption objIWebEncryption = new IWebEncryption(); 
                    strPassword = objIWebEncryption.Encryption( this.txtPassword.Text, this.txtKey.Text ); 
                    strPassword = HttpUtility.UrlEncode( strPassword ); 
                } 
                else 
                { 
                    strPassword = this.txtPassword.Text; 
                } 
                return strPassword; 
            } 
        } 
        #endregion 
        
        private void btnEncrypt_Click( System.Object sender, System.EventArgs e ) 
        { 
            string strResponse = ""; 
            IWeb.Url = WebserviceURL; 
            IWeb.IWebAuthendicationHeaderValue = IWebAuthendication.AttachCredentials(); 
            try 
            { 
                strResponse = IWeb.EncryptData( TextBox1.Text, this.txtKey.Text ); 
                this.TextBox2.Text = HttpUtility.UrlDecode( strResponse ); 
            } 
            catch ( Exception ex ) 
            { 
                TextBox2.Text = ex.Message; 
            } 
        } 
        
        
        private void btnUnEncrypt_Click( System.Object sender, System.EventArgs e ) 
        { 
            string strResponse = ""; 
            IWeb.Url = WebserviceURL; 
            IWeb.IWebAuthendicationHeaderValue = IWebAuthendication.AttachCredentials(); 
            try 
            { 
                strResponse = IWeb.UnEncryptData( HttpUtility.UrlEncode( TextBox1.Text ), this.txtKey.Text ); 
                this.TextBox2.Text = HttpUtility.UrlDecode( strResponse ); 
            } 
            catch ( Exception ex ) 
            { 
                TextBox2.Text = ex.Message; 
            } 
        } 
        
        
        private void btnGetVersion_Click( System.Object sender, System.EventArgs e ) 
        { 
            IWebVersion_Info IWebVersion_Info = null; 
            try 
            { 
                IWebVersion_Info = IWebVersion.GetVersion(); 
                this.lblGetVersion.Text = "Version: " + IWebVersion_Info.VersionName + " (" + IWebVersion_Info.VersionNumber.ToString() + ")"; 
            } 
            catch ( Exception ex ) 
            { 
                this.lblGetVersion.Text = ex.Message; 
            } 
        } 
        
        
        private void btnPortals_Click( System.Object sender, System.EventArgs e ) 
        { 
            int i = 0; 
            string[] arrPortals = null; 
            string colPortals = ""; 
            string strPortalNumber = null; 
            string strPortalName = null; 
            string strPortalEntry = null; 
            try 
            { 
                IWeb.Url = WebserviceURL; 
                IWeb.IWebAuthendicationHeaderValue = IWebAuthendication.AttachCredentials(); 
                
                arrPortals = IWeb.PortalsOnServer().Split( char.Parse( "," ) ); 
                for ( i=0; i <= arrPortals.Length - 1; i+=2 ) 
                { 
                    strPortalNumber = arrPortals[i].ToString(); 
                    strPortalName = arrPortals[i + 1].ToString(); 
                    strPortalEntry = strPortalNumber + " [" + strPortalName + "]"; 
                    colPortals = colPortals + strPortalEntry + Constants.vbCrLf; 
                } 
                lblPortals.Text = colPortals.ToString(); 
            } 
            catch ( Exception ex ) 
            { 
                this.lblGetVersion.Text = ex.Message; 
            } 
        } 
        
        
        private void btnCheckVersion_Click( System.Object sender, System.EventArgs e ) 
        { 
            if ( IWebVersion.CheckVersion( 10100 ) ) 
            { 
                MessageBox.Show( "The IWeb version on the server is correct" ); 
            } 
            else 
            { 
                MessageBox.Show( "The IWeb version on the server is too low" ); 
            } 
        } 
        
        
        /* TRANSINFO: Default instance for Form */
        private static IWeb_Form transDefaultFormIWeb_Form = null;
        public static IWeb_Form TransDefaultFormIWeb_Form
        { 
        	get
        	{ 
        		if (transDefaultFormIWeb_Form == null)
        		{
        			transDefaultFormIWeb_Form = new IWeb_Form();
        		}
        		return transDefaultFormIWeb_Form;
        	} 
        }

        private void btnGetUser_Click(object sender, EventArgs e)
        {
            IWebservice.UserInfo objUserInfo;

            try
            {
                IWeb.Url = WebserviceURL;
                IWeb.IWebAuthendicationHeaderValue = IWebAuthendication.AttachCredentials();
                objUserInfo = IWeb.GetUser(txtUser.Text);

                lblShowDisplayName.Text = objUserInfo.DisplayName;
                lblShowFirstName.Text = objUserInfo.FirstName;
                lblShowLastName.Text = objUserInfo.LastName;
                lblShowEmail.Text = objUserInfo.Email;
                lblShowPassword.Text = objUserInfo.Membership.Password;
            }

            catch (Exception ex)
            {
                this.lblShowDisplayName.Text = ex.Message;
            }

        }

        private void btnCreateUser_Click(object sender, EventArgs e)
        {
            try
            {
                IWeb.Url = WebserviceURL;
                IWeb.IWebAuthendicationHeaderValue = IWebAuthendication.AttachCredentials();

                lblCreateStatus.Text = IWeb.CreateUser(txtSetUserName.Text, txtFirstName.Text, txtLastName.Text, txtDisplayName.Text, txtEmail.Text, txtSetPassword.Text);
            }

            catch (Exception ex)
            {
                lblCreateStatus.Text = ex.Message;
            }
        }


    } 
    
    
} 
